<template>
	<view>
		<view class="wrapper">
			<u-gap height="30"></u-gap>
			<view class="wrapper font-32 font-bold">
				Attachments
			</view>
			<u-gap height="20"></u-gap>
			<view class="box wrapper">
				<u-gap height="20"></u-gap>
				<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="9"></u-upload>
				<u-gap height="20"></u-gap>
			</view>

			<u-gap height="30"></u-gap>
			<view class="fixed-bottom-box" style="width: 426upx;margin: 0 auto;">
				<u-button type="primary" size="large" text="SUBMIT" @click="submit"></u-button>
				<u-gap height="30"></u-gap>
			</view>
			<u-gap height="15"></u-gap>
		</view>
	</view>
</template>

<script>
	import {
		resume_sid_me
	} from '@/common/api.js';
	export default {
		data() {
			return {
				fileList: []
			};
		},
		watch: {

		},
		methods: {
			// 删除图片
			async deletePic(event) {
				console.log(event)
				let imgIndex = Number(event.index);
				// await this.deleteFilePromise(imgIndex);
				this.fileList.splice(imgIndex, 1);
				this.onFileChange();
			},
			deleteFilePromise(imgIndex) {
				// return unlink_attachment({
				// 	res_model,
				// 	res_id,
				// 	res_field: 'img' + (imgIndex + 1),
				// 	csrf_token: this.userInfo.csrf_token
				// }).then((res) => {
				// 	console.log(res);
				// })
			},

			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this.fileList.length;
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url, fileListLen);
					console.log(result);
					let item = this.fileList[fileListLen];
					let fullurl = this.getImgSrc(result.url);
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: fullurl,
						shortUrl: result.url
					}))
					fileListLen++
				}
				this.onFileChange();
			},
			onFileChange() {
				// let urls = this.fileList.map(v=>v.shortUrl);
				// this.$emit('update', urls,this.name);
			},
			submit() {
				let images = this.fileList.map(v => {
					return {
						cover: v.shortUrl,
						status: 0,
						type: 0
					}
				});
				console.log(images);
				let data = {
					key: 'images',
					value: images
				};
				uni.$emit('form_update', data);
				this.navBack();
			},
			getData() {
				// if (!this.sid) {
				// 	return false;
				// }
				// uni.showLoading({
				// 	title: '加载中'
				// });
				// resume_sid_me(this.sid).then(async (res) => {
				// 	let {
				// 		images
				// 	} = res.data;
				// 	if(images && images.length>0){
				// 		this.fileList = images.map(v=>{
				// 			let fullurl = this.getImgSrc(v.cover);
				// 			return {
				// 				url:fullurl
				// 			}
				// 		});
				// 	}

				// }).finally(() => {
				// 	uni.hideLoading();
				// 	uni.stopPullDownRefresh();
				// });
			}
		},
		onReady() {

		},
		onShow() {
			uni.$off('refresh_upload');
			uni.$once('refresh_upload', (v) => {
				this.getData();
			});
		},
		onLoad(d) {
			let images = JSON.parse(d.data||'[]') || [];
			this.fileList = images.map(v => {
				let fullurl = this.getImgSrc(v.cover);
				return {
					url: fullurl,
					shortUrl:v.cover
				}
			});
			this.getData();
		},
		onPullDownRefresh() {
			// this.getData();
		},
		onReachBottom() {

		}
	}
</script>

<style lang="scss">

</style>